# -*- coding: utf-8 -*-
import logging

from selenium.common.exceptions import TimeoutException, NoSuchElementException

from pages.base_page import BasePage
from objectsmap.page_locators import Login
from pages_exceptions.pages_exceptions import LoginException


class LoginPage(BasePage):
    TITLE = 'IASO Login'
    LOCATION = 'https://gfisoftware.force.com/login'

    def __init__(self, driver):
        super(LoginPage, self).__init__(driver, LoginPage.LOCATION)

    def is_right_location(self):
        super(LoginPage, self).is_right_location(LoginPage.TITLE)

    def login(self, user):
        try:
            username_element = self._driver.find_element(*Login.IN_USERNAME)
            username_element.send_keys(user['login'])
            password_element = self._driver.find_element(*Login.IN_PASSWORD)
            password_element.send_keys(user['password'])
            # self._driver.find_element(Login.BTN_SUBMIT).submit()
            logging.info('Login success')
        except (TimeoutException, NoSuchElementException) as e:
            raise LoginException('Can\'t login')